package com.lg.system.main.controller;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.main.entity.Shiro;
import com.lg.system.main.service.ShiroService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-03-30
 */
@RestController
@RequestMapping("/shiro")
public class ShiroController extends BaseController<Shiro, ShiroService> {

    private final String prefix="system/main/shiro/";
    
	@Autowired
	ShiroService shiroService;

	@RequiresPermissions("shiro:shiro")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("shiro:shiro")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody Shiro  shiro){
		Query<Shiro> query = new Query<Shiro>(shiro);
		List<Shiro> shiros=shiroService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(shiros,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("shiro:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	@RequiresPermissions("shiro:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", shiroService.selectById(id));
		return mView;
	}
	
	@RequiresPermissions("shiro:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) Shiro shiro,BindingResult bindingResult){
		if(shiroService.save(shiro)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("shiro:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) Shiro shiro,BindingResult bindingResult){
		if(shiroService.update(shiro)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("shiro:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) Shiro shiro,BindingResult bindingResult){
		
		if(shiroService.deleteById(shiro.getId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("shiro:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(shiroService.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("shiro:edit")
	@PostMapping("/updateRows")
	public R updateRows(@RequestParam(value="ids[]") List<Long> ids,Integer type){
		if(shiroService.updateRows(ids,type)){
			return R.ok();
		}else{
			return R.error();
		}
	}

}
